//
//  BaseViewController.swift
//  Owner
//
//  Created by Selina on 1/7/2020.
//  Copyright © 2020 Selina. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

enum ControllerNaviState {
    case willPush,didPush,willPop,didPop
}

class BaseViewController: UIViewController {
    
    var statusBarStyle: UIStatusBarStyle = .default {
        didSet {
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
    //普通回调
    var callBack: CallBack?
    func execCallBack(block:@escaping CallBack){
        callBack = block
    }
    //返回回调
    var returnCallBack : ((_ result: ControllerNaviState?) -> Void)?
    func execReturnCallBack(block:@escaping CallBack){
        returnCallBack = block
    }
    //viewmodel
    lazy var disposeBag = DisposeBag()

    private lazy var emptyView: EmptyView = {
        let view = EmptyView()
        return view
    }()
    
    func showEmptyView(image: UIImage?, msg: String?, subMsg: String?, _ callBack: CallBack?) {
        self.view.addSubview(self.emptyView)
        self.emptyView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        self.emptyView.show(image: image, msg: msg, subMsg: subMsg, callBack)
    }
    
    func hideEmptyView() {
        self.emptyView.hide()
    }
    
    convenience init(_ callBack: CallBack?) {
        self.init()
        self.callBack = callBack
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Theme.kBackgroundColor
        
        // scrollview扩展到顶部
        //        self.automaticallyAdjustsScrollViewInsets = false
        
        // view扩展到导航栏底部
        self.extendedLayoutIncludesOpaqueBars = true
        
        addSubViews()
        addViewLayout()
        addViewModel()
    }
    
    deinit {
        Log.verbose("==========\(self)")
    }
    // MARK: - =====================addViews====================
    func addSubViews() {}
    // MARK: - =====================addLayout====================
    func addViewLayout(){}
    // MARK: - =====================添加viewmodel====================
    func addViewModel() {}
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return statusBarStyle
    }
    // MARK: - =====================页面的生命周期====================
    override func willMove(toParent parent: UIViewController?) {
        super.willMove(toParent: parent)
        
        if parent != nil{
            if self.returnCallBack != nil {
                self.returnCallBack!(ControllerNaviState.willPush)}
        }else{
            if self.returnCallBack != nil {
                self.returnCallBack!(ControllerNaviState.willPop)}
        }
        
    }
    override func didMove(toParent parent: UIViewController?) {
        super.didMove(toParent: parent)
        if parent != nil{
            if self.returnCallBack != nil {
                self.returnCallBack!(ControllerNaviState.didPush)}
        }else{
            if self.returnCallBack != nil {
                self.returnCallBack!(ControllerNaviState.didPop)}
        }
    }
    
    override func didReceiveMemoryWarning() {
        cleanSDWebimageCache()
    }
}
